package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVehicleOtherSkill;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserVehicleOtherSkillDaoImpl extends TempDataObjectDao<UserVehicleOtherSkill> {

	public List<UserVehicleOtherSkill> getUserVehicleOtherSkillList(long userId, String vehicleUid) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_vehicle_other_skill where userId=? and vehicleUid = ?";
		List<UserVehicleOtherSkill> dataList = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setString(2, vehicleUid);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleOtherSkill data = parse(rs);
				dataList.add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public Map<String, List<UserVehicleOtherSkill>> getUserVehicleOtherSkillList(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_vehicle_other_skill where userId=?";
		Map<String, List<UserVehicleOtherSkill>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleOtherSkill data = parse(rs);
				if (!dataMap.containsKey(data.getVehicleUid())) {
					dataMap.put(data.getVehicleUid(), new ArrayList<>());
				}
				dataMap.get(data.getVehicleUid()).add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserVehicleOtherSkill data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_vehicle_other_skill(userId,vehicleUid,skillType,skillId,skillLv)" +
				" VALUES(?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getVehicleUid());
			call.setInt(i++, data.getSkillType());
			call.setInt(i++, data.getSkillId());
			call.setInt(i++, data.getSkillLv());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserVehicleOtherSkill data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_vehicle_other_skill SET skillLv=? WHERE userId=? "
				+ "and vehicleUid=? and skillId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getSkillLv());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getVehicleUid());
			call.setInt(i++, data.getSkillId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserVehicleOtherSkill parseData(ResultSet rs) throws SQLException {
		UserVehicleOtherSkill data = new UserVehicleOtherSkill();
		data.setUserId(rs.getLong("userId"));
		data.setVehicleUid(rs.getString("vehicleUid"));
		data.setSkillType(rs.getInt("skillType"));
		data.setSkillId(rs.getInt("skillId"));
		data.setSkillLv(rs.getInt("skillLv"));
		return data;
	}
}
